﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IBiz;

namespace GuRenFang.Controllers.WebSite
{
    [ErrorHanderFilter]
    public class ActivitiesController : Controller
    {
        private readonly IActivitiesBiz _activitiesService;

        public ActivitiesController(IActivitiesBiz activitiesService)
        {
            _activitiesService = activitiesService;
        }

        [ChildActionOnly]
        [OutputCache(Duration = 3600)]
        public PartialViewResult _TopFive()
        {
            int totalRecord = 0;
            IEnumerable<Activities> topFiveNews = _activitiesService.GetItemsByPages(out totalRecord, 1, 10, "");
            return PartialView("_TopFive", topFiveNews);
        }

        public ActionResult List(int? page = 1)
        {
            short size = Convert.ToInt16(ConfigurationManager.AppSettings["pageSize"]);

            int totalRecord = 0;
            IEnumerable<Activities> activities = _activitiesService.GetItemsByPages(out totalRecord,
                                                                                    pageIndex: page.GetValueOrDefault(),
                                                                                    pageSize: size, orderFiled: "");

            var item = new PageItem
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages = (totalRecord%size == 0) ? totalRecord/size : totalRecord/size + 1,
                               ControllerName = "Activities",
                               ActionName = "List"
                           };

            var viewModels = new Pager<Activities> {DataSource = activities, Item = item};

            return View(viewModels);
        }

        public ActionResult Detail(int? id = 1)
        {
            Activities newsItem = _activitiesService.GetById(id.GetValueOrDefault());
            newsItem.ReadTimes++;
            _activitiesService.Update(newsItem);

            return View(newsItem);
        }
    }
}
